package com.wu.pdf.utils;

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;

public class PdfWatermarkRemover {

    public void removeWatermark(String pdfFilePath, String outputFilePath) throws IOException {
        PDDocument document = PDDocument.load(new File(pdfFilePath));
        for (PDPage page : document.getPages()) {
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
            contentStream.close();
        }
        document.save(outputFilePath);
        document.close();
    }

    public static void main(String[] args) {
        PdfWatermarkRemover remover = new PdfWatermarkRemover();
        try {
            remover.removeWatermark("/Users/xiaoxu.wu/Desktop/2019-2020年学年度第二学期 三年级数学 练习（全套）_加水印.pdf", "/Users/xiaoxu.wu/Desktop/output2.pdf");
            System.out.println("Watermark removed successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}